package com.apimarket.util.kong.impl;

import com.apimarket.entity.kong.AclCredential;
import com.apimarket.enums.InvalidTypeEnum;
import com.apimarket.exception.InvalidTypeException;
import com.apimarket.factory.JsonMapperFactory;
import com.apimarket.util.UrlUtil;
import com.apimarket.util.kong.KongConsumer;
import com.apimarket.util.kong.KongOperationType;
import com.apimarket.util.net.GetNetContext;
import com.apimarket.util.net.NewPostJsonNetContext;
import com.apimarket.util.net.PostJsonNetContext;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.type.CollectionType;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Consumer与acl插件绑定实现类
 *
 * @author yyc
 */
@Component
public class ConsumerBindAcl extends KongConsumer<AclCredential> {
    private static final String bindPath = "/acls";

    public ConsumerBindAcl() {
    }

    public ConsumerBindAcl(String baseUrl) {
        super(baseUrl);
    }

    @Override
    public AclCredential bindPlugin(String nameOrId, Object... args) throws IOException, URISyntaxException {
        if (args.length != 1) throw new InvalidTypeException(InvalidTypeEnum.METHOD_PARAMETER_ERROR);
        String targetPath = getBaseUrl() + bindPrefix + "/" + nameOrId + bindPath;
        Map<String, Object> params = new HashMap<>();
        params.put("group", args[0]);
        UrlUtil urlUtil = new UrlUtil(new PostJsonNetContext(targetPath, params));
        JsonNode root = urlUtil.getJsonRoot();
        return JsonMapperFactory.getInstance().readValue(root.toString(), AclCredential.class);
    }

    public AclCredential addGroup(String id,String group) throws IOException, URISyntaxException {
        String targetPath = getBaseUrl() + bindPrefix + "/" + id + bindPath;
        Map<String,Object> params=new HashMap<>();
        params.put("group",group);
        NewPostJsonNetContext newPostJsonNetContext = new NewPostJsonNetContext(targetPath, params);
        JsonNode root = newPostJsonNetContext.getResultJsonAndJudge(KongOperationType.CONSUMER_GROUP_ADD);
        return JsonMapperFactory.getInstance().readValue(root.toString(),AclCredential.class);
    }

    @Override
    public List<AclCredential> listAllBindPluginDetail() throws IOException, URISyntaxException {
        String targetPath = getBaseUrl() + bindPath;
        return getCredentialList(targetPath);
    }

    @Override
    public List<AclCredential> listBindPluginDetailViaConsumer(String nameOrId) throws IOException, URISyntaxException {
        String targetPath = getBaseUrl() + bindPrefix + "/" + nameOrId + bindPath;
        return getCredentialList(targetPath);
    }

    private List<AclCredential> getCredentialList(String targetUrl) throws IOException, URISyntaxException {
        UrlUtil urlUtil = new UrlUtil(new GetNetContext(targetUrl, null));
        JsonNode root = urlUtil.getJsonRoot();
        root = root.get("data");
        if (root == null) return null;
        ObjectMapper mapper = JsonMapperFactory.getInstance();
        CollectionType listType = mapper.getTypeFactory().constructCollectionType(ArrayList.class, AclCredential.class);
        return mapper.readValue(root.toString(), listType);
    }
}
